package com.gardenguell.salad.task;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import com.gardenguell.salad.R;
import com.gardenguell.salad.app.AppConstant;
import com.gardenguell.salad.app.AppContext;
import com.gardenguell.salad.data.cache.CacheService;
import com.gardenguell.salad.data.cache.MessageDBManager;
import com.gardenguell.salad.data.client.SaladClient;
import com.gardenguell.salad.data.model.Box;
import com.gardenguell.salad.data.model.Unit;
import com.gardenguell.salad.data.model.Vegetable;
import com.gardenguell.salad.data.volley.OnErrorResponse;
import com.gardenguell.salad.data.volley.OnSuccessResponse;
import com.gardenguell.salad.ui.activity.MessageActivity;
import com.gardenguell.salad.util.CommonLog;
import com.gardenguell.salad.util.NotificationUtil;
import com.gardenguell.salad.util.SharedPreferenceUtil;

import java.util.List;

/**
 * Created by absurd on 16/5/28.
 */
public class CheckUnitStatusTask extends AsyncTask<Void, Void, Void>
        implements OnErrorResponse{


    private SaladClient mClient = new SaladClient();

    private Context mContext;

    private CacheService cacheService = CacheService.getInstance(AppContext.getContext());

    public CheckUnitStatusTask(Context context){
        this.mContext = context;
    }


    @Override
    protected Void doInBackground(Void... params) {
        final String username = SharedPreferenceUtil.getInstance().get(AppConstant.PREF_TOKEN.AUTH_USERNAME, "");
        if(username.equals("")){
            return null;
        }
        mClient.getBoxList(username, new OnSuccessResponse<Box>() {
            @Override
            public void onSuccessWithList(List<Box> list) {
                super.onSuccessWithList(list);
                for(int i = 0; i < list.size(); i++){
                    Box b = list.get(i);
                    mClient.getUnitList(b.getMac(), username, new OnSuccessResponse<Unit>() {
                        @Override
                        public void onSuccessWithList(List<Unit> list) {
                            super.onSuccessWithList(list);
                            for(int j = 0; j < list.size(); j++){
                                // 如果满足条件.
                                Unit u = list.get(j);
                                CacheService.getInstance(mContext).messageDBManager.insertMessage(u.getInstanceId(), u.getVegetableId());
                            }
                        }
                    }, CheckUnitStatusTask.this);
                }
            }
        }, this);


        // 推送notification
        int notificationId = 1;
        List<MessageDBManager.Message> notificationMessages = cacheService.messageDBManager.getUnReadMessages();
        Intent intent = new Intent(mContext, MessageActivity.class);
        for(MessageDBManager.Message m : notificationMessages){
            // String message = "Unit" + (j+1) + vegetableName + "可以采摘啦";
            notificationId = notificationId + 1;
            NotificationUtil.notification(mContext, intent, notificationId, R.mipmap.ic_launcher,
                    "采摘提醒","采摘提醒", notificationId + "");
        }
        return null;
    }

    @Override
    public void onError(int errorCode) {

    }
}